var can1;
var can2;

var ctx1;
var ctx2;

var canWidth=800;
var canHeight=600;

var lastTime;
var deltaTime;

var bgPic=new Image();

var ane;

var fruit;

var mom;

var son;

var mx;
var my;

var babyEyes=[];
var babyBodys=[];

var momEyes=[];
var momBodyOra=[];
var momBodyBlue=[];

var data;

var wave;

var dust;
var dustPic=[];

document.body.onload=game;
function game()
{
	init();
	lastTime=Date.now();
	deltaTime=0;
	gameloop();
}
function init()
{
	//获取canvas context
	can1=document.getElementById("canvas1");//fishes,dust,UI,circle
	ctx1=can1.getContext('2d');
	can2=document.getElementById("canvas2");//background,ane,fruits
	ctx2=can2.getContext('2d');
	
	can1.addEventListener('mousemove',onMouseMove,false);
	
	bgPic.src="./src/background.jpg";
	
	canWidth=can1.width;
	canHeight=can1.height;
	
	ane=new aneObj();
	ane.init();
	
	fruit=new fruitObj();
	fruit.init();
	
	mom=new momObj();
	mom.init();
	
	son=new sonObj();
	son.init();
	
	mx=canWidth*0.5;
	my=canHeight*0.5;
	
	for(var i=0;i<2;i++)
	{
		babyEyes[i]=new Image();
		babyEyes[i].src="./src/babyEye"+i+".png";
	}
	
	for(var i=0;i<2;i++)
	{
		momEyes[i]=new Image();
		momEyes[i].src="./src/bigEye"+i+".png";
	}
	
	for(var i=0;i<20;i++)
	{
		babyBodys[i]=new Image();
		babyBodys[i].src="./src/babyFade"+i+".png";
	}
	
	for(var i=0;i<8;i++)
	{
		momBodyOra[i]=new Image();
		momBodyBlue[i]=new Image();
		momBodyOra[i].src="./src/bigSwim"+i+".png";
		momBodyBlue[i].src="./src/bigSwimBlue"+i+".png";
	}
	
	data=new dataObj();
	
	ctx1.fillStyle="white";
	ctx1.font="30px Verdana";
	ctx1.textAlign="center";
	
	wave=new waveObj();
	wave.init();
	
	for(var i=0;i<7;i++)
	{
		dustPic[i]=new Image();
		dustPic[i].src="./src/dust"+i+".png";
	}
	
	dust=new dustObj();
	dust.init();
	
}
function gameloop()
{
	requestAnimFrame(gameloop);//比setInterval,setTimeout更科学，因为他是智能绘制，根据计算机的性能判断绘制间隔，缺点：时间间隔不确定
	var now=Date.now();
	deltaTime=now-lastTime;
	lastTime=now;
	if(deltaTime>50) deltaTime=40;
	
	drawbackground();
	ane.draw();
	fruitMonitor();
	fruit.draw();
	ctx1.clearRect(0,0,canWidth,canHeight);
	mom.draw();
	son.draw();
	data.draw();
	wave.draw();
	dust.draw();
	
	if(data.gameOver==true) return;
	momFruitsCollision();
	momBabyCollision();
}

function onMouseMove(e)
{
	if(data.gameOver==true) return;
	if(e.offSetX||e.layerX)
	{
		mx=e.offSetX==undefined?e.layerX:e.offSetX;
		my=e.offSetX==undefined?e.layerY:e.offSetY;
	}
}